//
//  MyWalletDetailVC.m
//  InviteJob
//
//  Created by LangLang on 2017/12/19.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "MyWalletDetailVC.h"
#import "WithdrawRecordCell.h"
#import "MyWalletPaymentDetailsTableVC.h"
#define Screen_Width   ([[UIScreen mainScreen] bounds].size.width)
#define Screen_Height  ([[UIScreen mainScreen] bounds].size.height)
#define HeaderHeight 150
#define HeadImageViewHeight Screen_Width/1.5
@interface MyWalletDetailVC ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger selectTag;
    NSArray*titleArr;
    UIView*sectionView;
    NSString *_type;
    UILabel*  signatureLabel;
    UIView *viewBtnLine;
}
@property (nonatomic,assign)  NSInteger page;
@property (nonatomic,strong) NSString *perpage;
@property (nonatomic,strong) NSMutableArray *dataArr;
@property (nonatomic, strong) UIButton *navRightButton;

@end

@implementation MyWalletDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    
    TitleView(@"我的钱包")
    
    // right item
    self.navRightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    [self.navRightButton setTitle:@"充值" forState:UIControlStateNormal];
    self.navRightButton.titleEdgeInsets = UIEdgeInsetsMake(0,10, 0, -10);
    self.navRightButton.titleLabel.font = [UIFont systemFontOfSize:16];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:self.navRightButton];
    self.navigationItem.rightBarButtonItem = rightItem;
    // 事件绑定
    [self.navRightButton addTarget:self action:@selector(navdeleAction) forControlEvents:UIControlEventTouchUpInside];
    
    _page = 1;
    _type = @"in";
    self.perpage = @"10";
    [self postData];
    selectTag=0;
    titleArr=@[@"收入明细",@"支出明细"];
    [self.view addSubview:[self imageview]];
    [self.view addSubview:[self tableView]];
    [self addHeadView];
    
    sectionView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, 50)];
    sectionView.backgroundColor=[UIColor whiteColor];
    UIButton*btn;
    for (int i=0; i<2; i++) {
        btn=[[UIButton alloc]initWithFrame:CGRectMake(i*Screen_Width/2, 0, Screen_Width/2, 50)];
        [btn setTitle:titleArr[i] forState:0];
        btn.tag=1000+i;
        [btn addTarget:self action:@selector(selectBtn:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitleColor:[UIColor colorWithHexString:@"#333"] forState:0];
        btn.titleLabel.font = [UIFont systemFontOfSize:16];
        [btn setTitleColor:NavBackColor forState:UIControlStateSelected];
        [sectionView addSubview:btn];
        if (i==0) {
            btn.selected=YES;
            self.selectedBtn=btn;
        }else{
            btn.selected=NO;
        }
        
        
    }
    UIView *viewLine = [[UIView alloc] initWithFrame:CGRectMake(0, 49, WIDTH, 1)];
    viewLine.backgroundColor = [UIColor colorWithHexString:@"#e5e5e5"];
    [sectionView addSubview:viewLine];
    
    viewBtnLine = [[UIView alloc] initWithFrame:CGRectMake(0, 49, WIDTH/2, 1)];
    viewBtnLine.backgroundColor = NavBackColor;
    [sectionView addSubview:viewBtnLine];
    
    __unsafe_unretained UITableView *tableView = self.tableView;
    tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = 1;
        [self postData];
    }];
    [tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _page++;
        [self postData];
    }];
    self.tableView.mj_footer.ignoredScrollViewContentInsetBottom = 30;
    
}
- (void)navdeleAction {
    MyWalletPaymentDetailsTableVC *payment = [MyWalletPaymentDetailsTableVC new];
    [self.navigationController pushViewController:payment animated:YES];
}

-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        self.dataArr = @[].mutableCopy;
    }
    return _dataArr;
}
-(UIImageView*)imageview{
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, HeadImageViewHeight)];
        _headImageView.image = [UIImage imageNamed:@"backgroug"];
        self.origialFrame = _headImageView.frame;
    }
    return _headImageView;
}
- (UITableView *)tableView{
    if (!_perTableView) {
        _perTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height-64) style:UITableViewStylePlain];
        _perTableView.delegate = self;
        _perTableView.dataSource = self;
        _perTableView.backgroundColor=[UIColor clearColor];
        _perTableView.showsVerticalScrollIndicator = NO;
        _perTableView.rowHeight = 63;
        
        [_perTableView registerClass:[WithdrawRecordCell class] forCellReuseIdentifier:@"WithdrawRecordCell"];
        
    }
    return _perTableView;
}

-(void)addHeadView{
    UIView*headView= [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, HeadImageViewHeight)];
    headView.backgroundColor =[UIColor clearColor];
    headView.userInteractionEnabled = YES;
    
    
    UILabel*labelMsg=[[UILabel alloc]init];
    labelMsg.font=[UIFont systemFontOfSize:14];
    labelMsg.text=@"当前剩余点券";
    labelMsg.textColor=[UIColor colorWithColor:[UIColor whiteColor] alpha:0.5];
    labelMsg.textAlignment=1;
    [headView addSubview:labelMsg];
    [labelMsg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(headView).offset(-15);
        make.centerX.equalTo(headView);
    }];
    
    signatureLabel=[[UILabel alloc]init];
    signatureLabel.font=[UIFont systemFontOfSize:35 weight:0.5];
    signatureLabel.textColor=[UIColor whiteColor];
    signatureLabel.textAlignment=1;
    [headView addSubview:signatureLabel];
    [signatureLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(headView).offset(+15);
        make.centerX.equalTo(headView);
    }];
    self.perTableView.tableHeaderView=headView;
  
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WithdrawRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WithdrawRecordCell"];
    BaseModel *model = _dataArr[indexPath.row];
    cell.titleLabel.text = model.mark;
    cell.timeLabel.text = model.trans_time;
    
    
    if (selectTag==1000) {
        cell.contentLabel.text = [NSString stringWithFormat:@"+%@点",model.amont];
    }else{
        cell.contentLabel.text = [NSString stringWithFormat:@"-%@点",model.amont];
    }
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
    
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return sectionView;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //往上滑动offset增加，往下滑动，yoffset减小
    CGFloat yoffset = scrollView.contentOffset.y;
    //处理背景图的放大效果和往上移动的效果
    if (yoffset>0) {//往上滑动
        
        _headImageView.frame = ({
            CGRect frame = self.origialFrame;
            frame.origin.y = self.origialFrame.origin.y - yoffset;
            frame;
        });
        
    }else {//往下滑动，放大处理
        _headImageView.frame = ({
            CGRect frame = self.origialFrame;
            frame.size.height = self.origialFrame.size.height - yoffset;
            frame.size.width = frame.size.height*1.5;
            frame.origin.x = _origialFrame.origin.x - (frame.size.width-_origialFrame.size.width)/2;
            frame;
        });
    }
}
-(void)selectBtn:(UIButton*)sender{
    selectTag=sender.tag;
    [_dataArr removeAllObjects];
    _page = 1;
    if (sender!= self.selectedBtn) {
        self.selectedBtn.selected = NO;
        sender.selected = YES;
        self.selectedBtn = sender;
       
    }else{
        self.selectedBtn.selected = YES;
    }
    
    viewBtnLine.center = CGPointMake(sender.center.x, sender.center.y+24);
    
    if (sender.tag == 1000) {
        _type = @"in";
        [self postData];
    }
    
    if (sender.tag == 1001) {
        _type = @"out";
        [self postData];
    }
    
    [self.perTableView reloadData];
    
}

-(void)postData{
    isUser
    NSString *strUrl = [NSString stringWithFormat:post_historyLists_api];
    
    NSDictionary *dicParameter = @{@"user_id":user.user_id,
                                   @"token":user.token,
                                   @"type":_type,
                                   @"page":[NSString stringWithFormat:@"%ld",_page],
                                   @"perpage":self.perpage};
    
    [PostNetWorking postNetWorkingStr:strUrl andparameters:dicParameter Success:^(id responseObject) {
        
      
       signatureLabel.text=responseObject[@"datas"][@"ticket"];
        
        [self noData:_page ReqresponseObject:responseObject lists:@"lists" dataArr:self.dataArr arrCount:responseObject[@"datas"][@"lists"] withTable:self.tableView];
        
    } failed:^(NSError *error) {
        [self endRefreshing:self.tableView];
    }];
}

// 加载无数据显示和加载数据


-(void)noData:(NSInteger)page ReqresponseObject:(id)responseObject lists:(NSString *)lists dataArr:(NSMutableArray *)dataArr  arrCount:(NSArray *)arrCount withTable:(UITableView *)DefineTableView{
    __unsafe_unretained UITableView *tableView = DefineTableView;
    
    int code = [responseObject[@"code"]intValue];
    
    if (code == 200) {
        if (page==1) {
            if (dataArr.count) {
                [dataArr removeAllObjects];
            }
            self.dataArr = [BaseModel parsingDateFromResultDict:responseObject andData:lists];
            
        }else {
            [dataArr addObjectsFromArray:[BaseModel parsingDateFromResultDict:responseObject andData:lists]];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            if (arrCount.count>0) {
                [self hideNoDataView];
                if (arrCount.count<10) {
                    tableView.mj_footer.hidden = YES;
                }else {
                    tableView.mj_footer.hidden = NO;
                }
            }else {
                tableView.mj_footer.hidden = YES;
                [self shopNoDataViewBackgroundColor:[UIColor clearColor] withImage:@"img_home_no_news" withLabelMsg:@"收入明细空空如也" withButton:@"" isY:1];
                
            }
            
            [self endRefreshing:self.tableView];
            
        });
    }else{
        dataArr = [NSMutableArray array];
        [self endRefreshing:self.tableView];
    }
    
}

-(void)endRefreshing:(UITableView *)DefineTableView{
    __unsafe_unretained UITableView *tableView = DefineTableView;
    [tableView.mj_header endRefreshing];
    [tableView.mj_footer endRefreshing];
    [tableView reloadData];
    
}

@end
